<?php
	
	session_start();
	$u_name= $_SESSION['u_name'];
	$u_email= $_SESSION['u_email'];
	$u_id= $_SESSION['u_id'];
	$for= $_SESSION['uid'];
	
	$group= $_SESSION['group'];
	mysql_connect("localhost","root","");
	mysql_select_db("chat")or die(mysql_error());
	
?>
<table border="1px" width="100%">
	
			<?php 
			if(!isset($_SESSION['uid'])){
			$m=mysql_query("SELECT * FROM chat_msg WHERE for_group_id='$group' ORDER BY `chat_msg`.`msg_id` DESC")or die(mysql_error());
				$g=mysql_query("SELECT g_name FROM user_group WHERE group_id='$group'");
				$gr=(mysql_fetch_array($g));
				$gro=$gr['g_name'];
				echo "hello $u_name chat with Group $gro";	
			}else{
				
				$m=mysql_query("SELECT * FROM chat_msg WHERE sender_u_id='$u_id' and for_u_id='$for' or sender_u_id='$for' and for_u_id='$u_id'  ORDER BY `chat_msg`.`msg_id` DESC")or die(mysql_error());
				$g=mysql_query("SELECT u_name FROM users WHERE u_id='$for'");
				$gr=(mysql_fetch_array($g));
				$gro=$gr['u_name'];
				echo "hello $u_name chat with $gro";
			
			}
				
				
				while ($ms=(mysql_fetch_assoc($m))) {
					$alid=$ms['sender_u_id'];?>
					
						<?php
							$u=mysql_query("select u_name from users where u_id='$alid'")or die(mysql_error());
							while($us=(mysql_fetch_assoc($u))){
							?><tr><td><?php 
							if ($u_name==($us['u_name'])) { echo  ">>Me";
								
							} else {
								echo $us['u_name']; } ?><<</td>
						
							<?php } ?><td width="60%">
						<?=$ms['msg']?></td>
						
						<td><?=date('g:i A', strtotime($ms['time']))?></br><?php }
				?>
			
				</td></tr>	
			</table>	
